#{extends 'main.html'/}
<div class="l-clear"></div>
<div id="maingrid" style="margin-top:1px"></div> <br />
<div style="display:none;"></div>
<div id="detail" style="display:none;"><form id="mainform" method="post"></form></div>

<script type="text/javascript">
    $(function () {
      
        var typeData = [{ type: 1, text: '中药' }, { type: 2, text: '西药'},{ type: 3, text: '中成药'},{ type: 4, text: '西成药'}];
      
        var manager, grid;

        function f_initGrid() 
        {
            grid = manager = $("#maingrid").ligerGrid({
                columns: [
                    { display: '编号', name: 'id', width: 50, type: 'int' },
                    { display: '名称', name: 'Name', width: 100, type: 'textarea' },
                    { display: '药品分类', name: 'Sort', width: 100, 
                        editor: { type:'select', data:typeData, valueColumnName:'Sort' },
                        render: function (item)
                        {  
                            if (parseInt(item.Sort) == 1) return '中药';
                            if (parseInt(item.Sort) == 2) return '西药';
                            if (parseInt(item.Sort) == 3) return '中成药';
                            if (parseInt(item.Sort) == 4) return '西成药';     
                            return '';
                        }
                    },
                    { display: '规格', name: 'Specification', width: 120, type: 'string', type: 'textarea'},
                    { display: '生产厂商', name: 'Factory', width: 120, type: 'string', type: 'textarea'},
                    { display: '价格', name: 'Price', width: 120, type: 'string', type: 'textarea'},
                    { display: '输入码', name: 'NamePY', width: 120, type: 'string', type: 'textarea'}
                ],

                url: "medicinal/json",  method: "GET",                       
                width: '100%',height:"80%",
                toolbar: { items: [
                        { text: '增加', click: addRow, icon: 'add' },
                        { line: true },
                        { text: '修改', click: modifyRow, icon: 'modify' },
                        { line: true },
                        { text: '删除', click: deleteRow, icon: 'delete' }
                    ] }
            });         	
        }
        
        f_initGrid();
        
        function f_reload() {
            grid.loadData();
        }
        
        function getSelected()
        { 
            var row = manager.getSelectedRow();
            if (!row) { alert('请选择行'); return; }
            alert(JSON.stringify(row));
        }
        
        function getData()
        { 
            var data = manager.getData();
            alert(JSON.stringify(data));
        }
        
        function addRow()
        {
            showDetail({}, false);
        }
        
        function modifyRow()
        {
            var selected = grid.getSelected();
            if (!selected) { LG.tip('请选择行!'); return }
          //var parent = grid.getParent(selected);
            showDetail({            
                id: selected.id,
                Name: selected.Name,
                Sort: selected.Sort,
                Specification: selected.Specification,
                Factory: selected.Factory,
                Price: selected.Price,
                NamePY: selected.NamePY,
                Unit: selected.Unit
            }, false);
        }
        
        function deleteRow()
        {
            jQuery.ligerDialog.confirm('确定删除吗?', function (confirm) {
                if (confirm) f_delete(); });
        }
        
        function f_delete() {
            var selected = grid.getSelected();
            if (selected) {
                $.ajax({
                    url: 'medicinal/' + selected.id,
                    type: 'DELETE',                  
                    loading: '正在删除中...',                  
                    success: function () {
                        //LG.showSuccess('删除成功');
                        LG.tip('删除成功!');
                        f_reload();
                    },
                    error: function (message) {
                        LG.showError(message);
                    }
                });
            }
            else {
                LG.tip('请选择行!');
            }
        }
        
        var detailWin = null, curentData = null, currentIsAddNew;
        var mainform = $("#mainform");
	    
        function showDetail(data, isAddNew)
        {
            currentData = data;
            currentIsAddNew = isAddNew;
            if (detailWin)
            {
                detailWin.show(); 
            }
            else
            {
                //创建表单结构
              
                mainform.ligerForm({
                    inputWidth: 280,
                    fields: [
                        {name: "medicial.id", type: "hidden" },
                        { display: "名称", name: "medicial.Name", newline: true, 
                            labelWidth: 100, width: 220, space: 30, type: "text", 
                            validate: { required: true, maxlength: 50} },
                        { display: "药品分类", name: "SortHide", comboboxName: "SortCombox", newline: true, 
                            labelWidth: 100, width: 220, space: 30, type: "select",
                            options: {data: typeData, valueField: 'type',textField :'text', valueFieldID: 'medicial.Sort'} },
                        { display: "规格", name: "medicial.Specification", newline: true, 
                            labelWidth: 100, width: 220, space: 30, type: "text" } ,
                        { display: "单位", name: "medicial.Unit", newline: true, 
                            labelWidth: 100, width: 220, space: 30, type: "text" },
                        { display: "生产厂家", name: "medicial.Factory", newline: true, 
                            labelWidth: 100, width: 220, space: 30, type: "text" },
                        { display: "价格", name: "medicial.Price", newline: true, 
                            labelWidth: 100, width: 220, space: 30, type: "text" },
                        { display: "输入码", name: "medicial.NamePY", newline: true, 
                            labelWidth: 100, width: 220, space: 30, type: "text" }                         
                    ],         			
                    toJSON: JSON2.stringify
                });
              
                //$("#DeptParentName").attr("readonly", "readonly");

                detailWin = $.ligerDialog.open({
                    target: $("#detail"),
                    width: 400, height: 300, top:80,
                    buttons: [
                        { text: '确定', onclick: function () { save(); } },
                        { text: '取消', onclick: function () { detailWin.hide(); } }
                    ]
                });
            }
          
            //if (!currentIsAddNew)
            //{    
               // loadData(currentData);
                $("[name$=id]").val(currentData.id);
                $("[name$=Name]").val(currentData.Name);
                $("[name='medicial.Sort']").val(currentData.type);
                $("[name$=Specification]").val(currentData.Specification);
                $("[name$=Unit]").val(currentData.Unit);
                $("[name$=Factory]").val(currentData.Factory);
                $("[name$=Price]").val(currentData.Price);
                $("[name$=NamePY]").val(currentData.NamePY);
               // $("#typeCombox").setValue(currentData.Sort);
                //$("[name$=name]").focus();
            //}

            function save()
            {
                $("#mainform").validate();
             
                $.ajax({
                    loading: '正在保存数据中...',
                    type: 'POST',
                    url: 'medicinals',                  
                    data: $("#mainform").serialize(),
                    success: function ()
                    {
                        detailWin.hide();
                        grid.loadData();
                        LG.tip('保存成功!');
                    },
                    error: function (message)
                    {
                        LG.tip(message);
                    }
                });
            }
        }
   
    })
</script>
